package com.reflact.test;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;

import org.lychie.jexcel.demo.model.FoursArea;


public class reflactTest2 {


/**

* <b>function:</b> 

* @project xcjpar

* @package com.xcj.common.util  

* @fileName @param args

* @createDate May 21, 2014 5:52:38 PM

* @author yy.niu

* @throws ClassNotFoundException 

* @throws IllegalAccessException 

* @throws IllegalArgumentException 

* @throws NoSuchFieldException 

* @throws SecurityException 

* @throws InvocationTargetException 

* @throws NoSuchMethodException 

* @throws InvocationTargetException 

*/

public static void main(String[] args) throws ClassNotFoundException, IllegalArgumentException, IllegalAccessException, SecurityException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException {
	
	FoursArea fa = new FoursArea();
	
	fa.setCode("0001");
	
	fa.setPartnerId(11);
	
	FoursArea fas = (FoursArea)testReflect(fa,FoursArea.class);
	
	System.out.println(fas.getModifyDate());
	
	System.out.println(fas.getDescription());
	
	System.out.println(fas.getLeader());
	
	System.out.println(fas.getMobile());
	
	System.out.println(fas.getCreateDate());
	
	System.out.println(fas.getName());


}

public static <T> T testReflect(Object model,Class<?> cals) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, SecurityException, NoSuchFieldException{

       Field[] field = model.getClass().getDeclaredFields();        //获取实体类的所有属性，返回Field数组  

           for(int j=0 ; j<field.length ; j++){     //遍历所有属性

               String nam = field[j].getName();    //获取属性的名字

               String name = nam;

               name  = name.substring(0,1).toUpperCase()+name.substring(1);

               System.out.println("attribute name:"+name);                

               String type = field[j].getGenericType().toString();    //获取属性的类型

               if(type.equals("class java.lang.String")){   //如果type是类类型，则前面包含"class "，后面跟类名

                   Method m = model.getClass().getMethod("get"+name);

                   String value = (String) m.invoke(model);    //调用getter方法获取属性值

                   if(value != null){

                       System.out.println("attribute value:"+value);

                   }else{

                   Field fielda = FoursArea.class.getDeclaredField(nam);

                   FoursArea fa = new FoursArea();

                   String  values = "1";

                      boolean accessible = fielda.isAccessible();

                      fielda.setAccessible(true);

                      fielda.set(fa, values);

                      fielda.setAccessible(accessible);

                      System.out.println(fa.getName());

 

                   }

                   

               }

               if(type.equals("class java.lang.Integer")){     

                   Method m = model.getClass().getMethod("get"+name);

                   Integer value = (Integer) m.invoke(model);

                   if(value != null){
                       System.out.println("attribute value:"+value);
                   }
               }

               if(type.equals("class java.lang.Short")){     

                   Method m = model.getClass().getMethod("get"+name);

                   Short value = (Short) m.invoke(model);

                   if(value != null){

                       System.out.println("attribute value:"+value);                    }

               }       

               if(type.equals("class java.lang.Double")){     

                   Method m = model.getClass().getMethod("get"+name);

                   Double value = (Double) m.invoke(model);

                   if(value != null){                    

                       System.out.println("attribute value:"+value);  

                   }

               }                  

               if(type.equals("class java.lang.Boolean")){

                   Method m = model.getClass().getMethod("get"+name);    

                   Boolean value = (Boolean) m.invoke(model);

                   if(value != null){                      

                       System.out.println("attribute value:"+value);

                   }

               }

               if(type.equals("class java.util.Date")){

                   Method m = model.getClass().getMethod("get"+name);                    

                   Date value = (Date) m.invoke(model);

                   if(value != null){

                       System.out.println("attribute value:"+value.toLocaleString());

                   }

               }                

           }

           return (T) model;

   }

 

 

 

 

}